package demo0;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Director director = new Director();
        Builder b1 = new ConcreteBuilder1();
        Builder b2 = new ConcreteBuilder2();

        director.Construct(b1);
        director.Construct(b2);
        Product p1 = b1.getResult();
        Product p2 = b2.getResult();

        p1.show();
        p2.show();
    }
}

class Product{
    List<String > parts = new ArrayList<>();
    public void Add(String part){//添加产品部件
        parts.add(part);
    }

    public void show(){
        System.out.println("\n 产品 创建 ----");
        for (String part:parts
             ) {
            System.out.println(part);
        }

    }
}

abstract class Builder{
    public abstract void BuildPartA();
    public abstract void BuildPartB();
    public abstract Product getResult();

}

class ConcreteBuilder1 extends Builder{
    private Product product = new Product();
    @Override
    public void BuildPartA() {
        product.Add("部件A");
    }

    @Override
    public void BuildPartB() {
        product.Add("部件B");
    }

    @Override
    public Product getResult() {
        return product;
    }
}
class ConcreteBuilder2 extends Builder{
    private Product product = new Product();
    @Override
    public void BuildPartA() {
        product.Add("部件C");
    }

    @Override
    public void BuildPartB() {
        product.Add("部件D");
    }

    @Override
    public Product getResult() {
        return product;
    }
}

class Director {
    //指挥者类
    public void Construct(Builder builder){
        builder.BuildPartA();
        builder.BuildPartB();
    }
}

